<?php

class MyForm extends Controller {

	function __construct()
	{
		parent::Controller();	
	}
	
	function index()
	{
		$this->load->database();
		$this->output->enable_profiler(TRUE);
		$this->db->query("select * from student");
		$this->load->view('welcome_message');
	}
	function formview($which)
	{
		$this->load->helper('form','url');
		
		
		$this->load->library('form_validation');
		$this->load->model("state");

		$this->form_validation->set_rules('first_name', 'First name', 'required');
		$this->form_validation->set_rules('last_name', 'Last Name', 'required');
		
		
		// Has this form been submitted yet?
		if($this->form_validation->run() == FALSE)
		{
			// If not let's give them the form that they just selected . . .
			// IE:  index.php/myform/formview/enroll pulls the one from the views folder called form_enroll.php
			$this->load->view("form_".$which);
		
		}
		else
		{
			// Let's load up the dbforge class just for grins here
			$this->load->dbforge();
			// Unset the post viables we don't need captured in the database
			unset($_POST['submit']);
			// For each of the post variables, let's make a loop
			foreach($_POST as $key=>$value)
			{
				// If the field doesn't exist we need to make the field in the database.
				// Ideally we should have a definition that allows us to intelligently decide what needs to be added.  
				// Perhaps in the first line of the view file have a comment line that gives all field definitions?
				if (!$this->db->field_exists($key, "student"))
				{
					// Here is where we create the new column -- again if we had a way to decide the field 
					// definition going into it we could fix that.
					$fields = array(
                        $key => array('type' => 'TEXT')
						);
					$this->dbforge->add_column('student', $fields);
				  
				}
			} 
			$this->db->insert('student', $_POST); 
			echo "INSERTED!";
		}
		
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */